package comet.vo;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.AsyncContext;

import com.google.gson.Gson;

import comet.util.SendMessageUntil;

public class Host {
	private AsyncContext async;
	private String userid;
	private String username;
	private String photo;
	private boolean online;
	private List<String> friends;
	public Host() {
		super();
	}
	public Host(AsyncContext async, String userid, String username,
			boolean online, List<String> friends) {
		super();
		this.async = async;
		this.userid = userid;
		this.username = username;
		this.online = online;
		this.friends = friends;
	}
	
	public String getPhoto() {
		return photo;
	}
	public void setPhoto(String photo) {
		this.photo = photo;
	}
	public boolean getOnline() {
		return online;
	}
	public void setOnline(boolean online) {
		this.online = online;
	}
	public List<String> getFriends() {
		return friends;
	}
	public void setFriends(List<String> friends) {
		this.friends = friends;
	}
	public String getUserid() {
		return userid;
	}
	public void setUserid(String userid) {
		this.userid = userid;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public AsyncContext getAsync() {
		return async;
	}
	public void setAsync(AsyncContext async) {
		this.async = async;
	}
	
	public void sendTo(Host h,String msg){
		h.receive(msg);
	}
	
	public void receive(String msg){
		try {
			System.out.println(username+"准备接收了"+msg);
			System.out.println(new Date());
			SendMessageUntil.sendMessage(async.getResponse().getWriter(), msg);
			System.out.println(new Date());
			System.out.println(username+"接收了"+msg);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public String hostToJson(int status){
		Map<String,Object> m = new HashMap<String,Object>();
		m.put("UID", userid);
		m.put("USERNAME", username);
		m.put("photo", photo);
		m.put("status", status);
		return new Gson().toJson(m);
	}
	
	public String toString(){
		return username+"!!!"+async;
	}
	
	
}
